package game.gamestate.debugroom;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import common.Coordinate;
import common.Log;
import common.Rectangle;

import engine.Constants;
import engine.Engine;
import engine.game.Game_I;
import game.entity.EntityGroup_I;
import game.entity.EntityType_E;
import game.entity.mob.Mob_Entity;
import game.entity.mob.SolidPrototype;
import game.entity.process.CollisionProcess;
import game.entity.process.EntityProcessor_I;
import game.entity.process.MovementProcess;
import game.entity.process.ThinkProcess;
import game.entity.process.UpdateProcess;
import game.gamestate.GameState;
import game.gamestate.debugroom.ui.BlockMapVisualizer;
import game.map.MapInfo;
import game.map.block.BlockMap;
import game.scene.Layer_E;
import game.scene.sceneobject.Rectangle_SO;

public class DebugRoom implements Game_I {
	public DebugRoom(GameState gameState) {
		if(gameState == null) {
			State = new GameState();
		} else {
			State = gameState;
		}
	}
	
	public final GameState State;
	public Rectangle_SO Cls;
	
	@Override
	public boolean initialize(Engine engine) {
		buildMap();
		Cls = new Rectangle_SO(new Rectangle(0,0,640,480));
		Cls.Color = 0;
		State.Scene.getLayer(Layer_E.BACKGROUND).insert(Cls);
		State.Entities.spawn(new BlockMapVisualizer(), State);
		for(int i = 0; i < 16; i++) {
			State.Entities.spawn(new SolidPrototype(new Coordinate(160, 160)), State);
		}
		return true;
	}

	@Override
	public boolean execute(Engine engine, int elapsed) {
		State.Entities.process(State, elapsed);
		State.Scene.render(null, engine.getScreenBuffer());
		return true;
	}

	@Override
	public boolean shutdown(Engine engine) {
		State.Entities.destroyAll(State);
		return true;
	}
	
	
	public void buildMap() {
		int w = 40;
		int h = 40;
		MapInfo mapInfo = new MapInfo(w, h, 8, 8);
		State.GameMap.redimension(mapInfo);
		
		BlockMap bMap = (BlockMap)State.GameMap.getBlockMap();
		Random rnd = new Random();
		for(int i = 0; i < 8; i++) {
			bMap.set(true, rnd.nextInt(w), rnd.nextInt(h));
		}
		for(int x = 0; x < w; x++) {
			bMap.set(true, x, 0);
			bMap.set(true, x, h - 1);
		}
		for(int y = 0; y < h; y++) {
			bMap.set(true, 0, y);
			bMap.set(true, w - 1, y);
		}
	}
}